<?php
//error_reporting(E_ALL);
//ini_set('display_errors', 1);

$this->useClass("Handler_Community");
class Action_Community_ThemeDelete extends Handler_Community {
  public function process() {
    $aa = $this->aa;
		$acl_data = $aa->getInfo();
		if (!$aa->is_granted('community_edit')) return;
    
    if ( isset($this->params['oid']) && !empty($this->params['oid']) ) {
      $rssmodel = $this->ctx->mm->getModel("RssFeed", "m");
      $rssmodel->setDisablePages();
      $rssfeed = $rssmodel->getRssFeedList($acl_data['comity_oid'], $this->params['oid']);
      
      if ( !empty($rssfeed) ) {
        foreach ($rssfeed as $feed) {
          $object = $this->ctx->store->getObject($feed['oid']);
          $object->is_deleted = 1;
          $object->save();
        }
      }
      
      $themesmodel = $this->ctx->mm->getModel("Themes", "m");
      $filters = $themesmodel->getFilters( $this->params['oid']);
      foreach ( $filters as $filter ) {
        $object = $this->ctx->store->getObject($filter['oid']);
        $object->is_deleted = 1;
        $object->save();
      }
      
      $theme = $this->ctx->store->getObject($this->params['oid']);
      $theme->is_deleted = 1;
      $theme->save();
      
      $this->rh->setMessage('Тема удалена.');
    }

    $location = "{$this->aa->acl_data['base_url']}/{$acl_data['community_url']}/themes_list";
    if ( isset($this->rh->GET['lpage']) && !empty($this->rh->GET['lpage']) ) {
      $lpage = (integer) abs($this->rh->GET['lpage']+0);
      if ( $lpage > 0 ) {
        $location .= "?lpage={$lpage}";
      }
    } else if ( isset($this->rh->GET['show_all']) && !empty($this->rh->GET['show_all']) ) {
      $location .= "?show_all=1";
    }
    header('Location: ' . $location);
    exit;
  }
}